package com.inossem.warehousestorage_inossem.activity.receive;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ListView;
import android.widget.TextView;

import com.inossem.loadandrefresh.PullToRefreshLayout;
import com.inossem.loadandrefresh.pullableview.PullableListView;
import com.inossem.utils.listener.NoDoubleClickListener;
import com.inossem.warehousestorage_inossem.R;
import com.inossem.warehousestorage_inossem.activity.base.BaseActivity;
import com.inossem.warehousestorage_inossem.activity.in_storage.InStorageManageActivity;
import com.inossem.warehousestorage_inossem.activity.receive.extra.ReceiveExtra;
import com.inossem.warehousestorage_inossem.adapter.receive.DeliveryListAdapter;
import com.inossem.warehousestorage_inossem.bean.receive.ReceiveManageBean;
import com.inossem.warehousestorage_inossem.listener.GsonBeanListener;
import com.inossem.warehousestorage_inossem.request.receive.ReceiveManageRequest;
import com.inossem.warehousestorage_inossem.utils.ActivityUtils;

import java.util.List;

//验收管理
public class ReceiveManageActivity extends BaseActivity {

    private TextView title;

    private PullToRefreshLayout refresh;

    private PullableListView deliveryList;

    private List<ReceiveManageBean.DataBean.DeliveryListBean> allData;

    private DeliveryListAdapter adapter;

    private String mDeliveryOrderNumber = "";

    private boolean mToInStorage = false;

    @Override
    protected void onCreateImpl(Bundle savedInstanceState) {
        setContentView(R.layout.activity_receive_manage);
        initView(ReceiveManageActivity.this);
    }

    @Override
    protected void onResumeImpl() {
        initData(ReceiveManageActivity.this);
    }

    private void initView(Activity activity) {
        title = (TextView) activity.findViewById(R.id.headerTitle);
        deliveryList = (PullableListView) activity.findViewById(R.id.deliveryList);
        refresh = (PullToRefreshLayout) activity.findViewById(R.id.deliveryRefresh);
    }

    private void initData(Activity activity) {
        title.setText(activity.getResources().getString(R.string.activity_receive_manage_title));
        ActivityUtils.initBackAndFunction(activity, R.string.activity_main_title_short, new NoDoubleClickListener() {
            @Override
            public void onNoDoubleClick(View v) {
                finish();
            }
        });

        deliveryList.setPullUpFlag(false);
        getExtra(activity);
        if (mToInStorage) {
            Intent intent = new Intent(activity, InStorageManageActivity.class);
            startActivity(intent);
            finish();
        } else {
            refresh.setOnRefreshListener(new PullToRefreshLayout.OnRefreshListener() {
                @Override
                public void onRefresh(PullToRefreshLayout pullToRefreshLayout) {
                    initData(activity);
                    pullToRefreshLayout.refreshFinish(PullToRefreshLayout.SUCCEED);
                }

                @Override
                public void onLoadMore(PullToRefreshLayout pullToRefreshLayout) {
                }
            });
            mDeliveryOrderNumber = "";
            getData(activity, mDeliveryOrderNumber);
        }

//        setmScanListener((String scanResult, int length) -> {
//            getData(activity, QRCodeUtils.DecryptQRCode(scanResult,QRCodeConstant.PARAMETER_SUPPLIER_NAME));
//        }, QRCodeConstant.TYPE_BATCH, QRCodeConstant.TYPE_MATERIEL_CODE, QRCodeConstant.TYPE_PURCHASE_ORDER, QRCodeConstant.TYPE_POSITION);
    }

    private void getExtra(Activity activity) {
        mToInStorage = activity.getIntent().getBooleanExtra(ReceiveExtra.TO_IN_STORAGE, false);
    }

    private void addDeliveryListData(Context context, List<ReceiveManageBean.DataBean.DeliveryListBean> deliveryItemList, ListView listView, boolean flag) {
        if (flag) {
            adapter = new DeliveryListAdapter(context, deliveryItemList, mDeliveryOrderNumber, (String deliveryOrderNumber) -> {
                mDeliveryOrderNumber = deliveryOrderNumber;
                getData(context, mDeliveryOrderNumber);
            });
            listView.setAdapter(adapter);
        }
    }

    private void getData(Context context, String condition) {
        ReceiveManageRequest.request(context, condition, new GsonBeanListener<ReceiveManageBean>() {
            @Override
            public void onSuccess(ReceiveManageBean bean) {
                allData = bean.getData().getDeliveryList();
                addDeliveryListData(context, ActivityUtils.getShowData(allData, 0), deliveryList, true);
            }
        });
    }

    public interface ReceiveManageListener {
        void onNoDoubleClickListener(String deliveryOrderNumber);
    }

}
